package com.logic.modular.game.battle.panel.skillEffect;

import com.logic.comment.util.BeanUtil;
import com.logic.modular.game.battle.panel.skillEffect.base.PvpSkillEffectBaseService;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 英雄战斗状态(pvpSkillEffect)表服务实现类
 *
 * @author cxy
 * @since 2024-08-22 11:35:32
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class PvpSkillEffectServiceImpl extends PvpSkillEffectBaseService {

    /**
     * 查看是否存在技能效果
     */
    public Boolean hasNoNumberEffectSkill(Long battleId, Long playerHeroId, Long coreSkillEffectTypeId) {
        boolean back = true;
        for (int i = 0; i < 4; i++) {
            back = back && has(battleId, playerHeroId, coreSkillEffectTypeId, i);
        }
        return back;
    }

    /**
     * 武将
     * 未触发的技能效果(被动,指挥)
     */
    public List<PvpSkillEffect> getAllNoTriggerSkillEffect(Long battleId, Long playerHeroId) {
        List<PvpSkillEffect> list = list(battleId, playerHeroId);
        return list.stream().filter(pvpSkillEffect -> BeanUtil.selector()
                .bool(
                        new ArrayList<Object>() {{
                            add(2);
                            add(3);
                        }}.contains(pvpSkillEffect.getCoreSkillTyp())
                ).eq(
                        5,
                        pvpSkillEffect.getStatus().getStatus()
                ).eq(
                        0,
                        pvpSkillEffect.getStatus().getNumberOfTriggers()
                )
                .value()
        ).collect(Collectors.toList());
    }

    public List<PvpSkillEffect> getAllNoTriggerSkillEffect(Long battleId) {
        return list(battleId).stream().filter(pvpSkillEffect -> BeanUtil.selector()
                .bool(
                        new ArrayList<Object>() {{
                            add(2);
                            add(3);
                        }}.contains(pvpSkillEffect.getCoreSkillTyp())
                ).eq(
                        5,
                        pvpSkillEffect.getStatus().getStatus()
                ).eq(
                        0,
                        pvpSkillEffect.getStatus().getNumberOfTriggers()
                )
                .value()
        ).collect(Collectors.toList());
    }


    /**
     * 获取所有可以触发的技能效果列表
     *
     * @param battleId 战斗id
     * @param round    回合
     * @return List<PvpSkillEffect>
     */
    public List<PvpSkillEffect> allTriggeringList(
            Long battleId, Integer round
    ) {
        return list(battleId).stream().filter(pvpSkillEffect -> BeanUtil.selector().eq(
                        5,
                        pvpSkillEffect.getStatus().getStatus()
                ).bool(
                        pvpSkillEffect.obtainNumberOfEffectiveRounds().contains(round)
                ).value()
        ).collect(Collectors.toList());
    }

    /**
     * 获取武将可以触发的技能效果列表
     *
     * @param battleId     战斗id
     * @param playerHeroId 玩家英雄id
     * @param round        回合
     * @return List<PvpSkillEffect>
     */
    public List<PvpSkillEffect> heroTriggeringList(
            Long battleId, Long playerHeroId, Integer round
    ) {
        return allTriggeringList(battleId, round).stream().filter(pvpSkillEffect -> BeanUtil.selector().eq(
                        playerHeroId,
                        pvpSkillEffect.getTargetPlayerHeroId()
                ).value()
        ).collect(Collectors.toList());
    }

    public Map<String, List<PvpSkillEffect>> obtainSkillEffects(Long battleId, Integer coreSkillTyp) {
        String key = String.format(SKILL_EFFECT_PANEL_KEY1, battleId);
        Set<Object> objects = redisUtils.hKeys(key);
        return objects.stream()
                .map(Object::toString)
                .filter(s -> s.endsWith("-" + coreSkillTyp))
                .map(s -> (PvpSkillEffect) redisUtils.hGet(key, s))
                .collect(Collectors.groupingBy(p -> "来源武将:" + p.getSourcePlayerHeroId() + "-"
                        + "技能id:" + p.getSourceCoreSkillId() + "-"
                        + "技能星阶id:" + p.getSourceCoreSkillStarId() + "-"
                        + "技能名称:" + p.getCoreSkillName() + "-"
                        + "[技能描述:" + p.getCoreSkillInfo() + "]"));
    }

    public void remove(PvpSkillEffect pvpSkillEffect) {
        log.info(
                "{}:移除成功,{}--->{}",
                pvpSkillEffect.getCoreSkillEffectTypeName(),
                pvpSkillEffect.getSourcePlayerHeroId(),
                pvpSkillEffect.getTargetPlayerHeroId()
        );
        remove(
                pvpSkillEffect.getBattleId(),
                pvpSkillEffect.getTargetPlayerHeroId(),
                pvpSkillEffect.getCoreSkillEffectId(),
                pvpSkillEffect.getCoreSkillTyp()
        );
    }
}
